//
//  JTTopWindow.m
//  百思不得姐
//
//  Created by Robin Zhang on 2017/1/20.
//  Copyright © 2017年 BeiJingWangDianJieTong. All rights reserved.
//

#import "JTTopWindow.h"

@implementation JTTopWindow

static UIWindow *window_;

+ (void)show {
    
    /*
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, JTScreenWidth, 20)];
    [topView setBackgroundColor:[UIColor redColor]];
    [topView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topStatusClick:)]];
    [self.window addSubview:topView];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        JTLog(@"%@", application.windows.lastObject.subviews.lastObject.subviews); // 弹键盘的类
    });
     */
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        // UIWindowLevelAlert > UIWindowLevelStatusBar > UIWindowLevelNormal
        // 相同级别的window，后来的window盖在先前window的上面
        
        // 添加一个最高级别的全局顶层window
        window_ = [[UIWindow alloc] initWithFrame:[UIApplication sharedApplication].statusBarFrame];
        [window_ setBackgroundColor:[UIColor clearColor]];
        [window_ setWindowLevel:UIWindowLevelAlert];
        [window_ addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topStatusClick:)]];
        [window_ setHidden:NO];
    });
}

+ (void)topStatusClick:(UITapGestureRecognizer *)sender {
    
    // 主窗口
    UIWindow *window = [UIWindow jt_highestVisibleWindow];
    
    NSMutableArray<UIScrollView *> *mutableArray = [NSMutableArray array];
    
    [UIView jt_findViewWithClass:[UIScrollView class] inView:window mutableArray:mutableArray];
    
    for (UIScrollView *scrollView in mutableArray) {
        
        if (![scrollView jt_intersectWithOtherView:window]) continue;
        
        CGPoint offset = scrollView.contentOffset;
        offset.y = - scrollView.contentInset.top;
        [scrollView setContentOffset:offset animated:YES];
        
        // [scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
    }
}

@end
